package com.work.formaldehyde.orderfragment;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.wang.avi.AVLoadingIndicatorView;
import com.work.formaldehyde.R;
import com.work.formaldehyde.activity.DingDanXiangQingActivity;
import com.work.formaldehyde.activityadapter.AllDingDanAdapter;
import com.work.formaldehyde.activityadapter.DaiPingJiaAdapter;
import com.work.formaldehyde.model.AllDingDanModel;
import com.work.formaldehyde.model.DPJDingDanModel;
import com.work.formaldehyde.util.ApiUtils;
import com.work.formaldehyde.util.Url;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class DPJFragment extends Fragment implements View.OnClickListener {
    //用于获取视图
    private View root;
    public static ListView daipingjialist;
    public static AVLoadingIndicatorView loding_avi;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_daipingjia, container, false);//初始加载
        if (ApiUtils.isNetworkConnected(getActivity()))
            InitView(root);
        return root;
    }

    public static ArrayList<AllDingDanModel.data> new_list_views;
    public static DaiPingJiaAdapter myAdapters;

    public void Init() {
        new_list_views = new ArrayList<>();
        myAdapters = new DaiPingJiaAdapter(new_list_views,
                getActivity(), getActivity());
        daipingjialist.setAdapter(myAdapters);
        okHttp_postFromParameters1();
    }

    /**
     * 初始加载视图
     */
    public void InitView(View view) {
        loding_avi = view.findViewById(R.id.loding_avi);
        loding_avi.show();
        daipingjialist = view.findViewById(R.id.daipingjialist);
        Init();
        daipingjialist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent tokpxq = new Intent(getActivity(), DingDanXiangQingActivity.class);
                tokpxq.putExtra("position", position + "");
                tokpxq.putExtra("username", new_list_views.get(position).getUser_name());
                tokpxq.putExtra("userphone", new_list_views.get(position).getUser_phone());
                tokpxq.putExtra("address", new_list_views.get(position).getUser_address());
                tokpxq.putExtra("ispay",new_list_views.get(position).getIspay());
                tokpxq.putExtra("type", "3");
                if (new_list_views.get(position).getGoods().size() > 1)
                    tokpxq.putExtra("duoge", "true");
                else
                    tokpxq.putExtra("duoge", "false");
                tokpxq.putExtra("shangpingimg", new_list_views.get(position).getGoods().get(0).getGoods_img());
                tokpxq.putExtra("shangpingname", new_list_views.get(position).getGoods().get(0).getGoods_name());
                tokpxq.putExtra("shangpingnum", new_list_views.get(position).getGoods().get(0).getGoods_num());
                tokpxq.putExtra("shangpingzongjia", new_list_views.get(position).getGoods().get(0).getGoods_discount_price());
                tokpxq.putExtra("dingdannum", new_list_views.get(position).getOrder_number());
                tokpxq.putExtra("xiadanshijian", new_list_views.get(position).getCreate_time());
                tokpxq.putExtra("zhifufangshi", new_list_views.get(position).getPay_type());
                tokpxq.putExtra("zhifushijian", new_list_views.get(position).getPay_time());
                tokpxq.putExtra("tuihuo", new_list_views.get(position).getOrder_status());
                startActivity(tokpxq, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        }
    }

    public static void okHttp_postFromParameters1() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
//                    RequestBody formBody = new FormBody.Builder()
//                            .add("uid", Url.USER_ID).add("status", "3").build();
//                    Request request = new Request.Builder().url(Url.GETDINGDANLIST).post(formBody).build();
//                    okhttp3.Response response = okHttpClient.newCall(request).execute();
//                    String msg = response.body().string();
//                    Message message = new Message();
//                    message.obj = msg;
//                    gethot.sendMessage(message);
                    Request request = new Request.Builder().get().url(Url.ADDDINGDANS+"?uid="+ Url.USER_ID + "&logintoken=" + Url.LOGIN_TOKEN+"&status=3").build();
                    Call call = okHttpClient.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                        }

                        @Override
                        public void onResponse(Call call, final Response response) {
                            try {
                                final String responseStr;
                                responseStr = response.body().string();
                                Message message = new Message();
                                message.obj = responseStr;
                                gethot.sendMessage(message);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (
                        Exception e) {
                    e.printStackTrace();
                }
            }
        }).
                start();
    }

    public static AllDingDanModel allDingDanModel;
    public static Handler gethot = new Handler() {
        public void handleMessage(android.os.Message msg) {
            JSONObject success_Object;
            try {
                if (msg.obj != null) {
                    loding_avi.hide();
                    success_Object = new JSONObject(msg.obj.toString());
                    String code = success_Object.getString("code");
                    if (code.equals("1")) {
                        Gson gson = new Gson();
                        allDingDanModel = gson.fromJson(msg.obj.toString(),
                                AllDingDanModel.class);
                        new_list_views.clear();
                        new_list_views.addAll(allDingDanModel.getData());
                        Collections.reverse(new_list_views);
                        myAdapters.notifyDataSetChanged();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

}